package org.electrical.controller.system;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Param;
import org.electrical.dao.EleRoleDao;
import org.electrical.pojo.EleRole;
import org.electrical.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/role")
@CrossOrigin(value = "*")
public class EleRoleController {

    @Autowired
    private EleRoleDao eleRoleDao;

    @RequestMapping("/search")
    @ResponseBody
    public List<EleRole> search() throws Exception{
        return eleRoleDao.getAll();
    }

    @RequestMapping("/list")
    @ResponseBody
    public ResponseResult<List<EleRole>> list(@RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "5")int limit, EleRole condition) throws Exception {

        Page<EleRole> pager = PageHelper.startPage(page, limit);
        List<EleRole> list = eleRoleDao.getConditionList(condition);
        return new ResponseResult(pager.getTotal(), list);

    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResponseResult delete(int id) throws Exception {

        eleRoleDao.delete(id);
        return ResponseResult.createSuccessResult();

    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    public ResponseResult batchDelete(Integer[] ids) throws Exception {

        eleRoleDao.batchDelete(ids);
        return ResponseResult.createSuccessResult();

    }

    @RequestMapping("/add")
    @ResponseBody
    public ResponseResult<EleRole> add(EleRole eleRole) throws Exception {

        eleRoleDao.add(eleRole);
        return ResponseResult.createSuccessResult();

    }

    @RequestMapping("/update")
    @ResponseBody
    public ResponseResult<EleRole> update(EleRole eleRole) throws Exception {

        eleRoleDao.update(eleRole);
        return ResponseResult.createSuccessResult();
    }

    @RequestMapping("/assignPers")
    @ResponseBody
    public ResponseResult assignPers(@Param("roleId") Integer roleId, @Param("perIds") String perIds) throws Exception {

        if (perIds != null && !perIds.equals("")) {
            String[] array = perIds.split(",");
            List<Integer> ids = new ArrayList<>();
            for (int i = 0; i < array.length; i++) {
                ids.add(Integer.parseInt(array[i]));
            }
            //  添加权限之前先删除之前已有的权限
            eleRoleDao.deleteRolePer(roleId);
            //  添加权限
            eleRoleDao.assignRights(roleId, ids);
        } else {
            eleRoleDao.deleteRolePer(roleId);
        }
        return ResponseResult.createSuccessResult();

    }

}
